package com.loongtech.bi.advertisment.controller;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.loongtech.bi.advertisment.model.ChannelRtData;
import com.loongtech.bi.advertisment.model.PayHourData;
import com.loongtech.bi.advertisment.model.PayMinData;
import com.loongtech.bi.advertisment.model.chart.AdvChartUIData1;
import com.loongtech.bi.advertisment.model.chart.AdvChartUIData2;
import com.loongtech.bi.advertisment.model.chart.AdvChartUIData3;
import com.loongtech.bi.advertisment.model.chart.AdvChartUIData4;
import com.loongtech.bi.advertisment.service.PayUserService;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;

/**
 * ClassName: ChannelDataAction
 * 
 * @author liuchen
 * @Description: 广告数据分析 - 实时：渠道数据
 * @date 2016-11-30 下午2:18:00
 */
@Controller("channelDataAction")
@Scope("view")
public class ChannelDataAction extends BaseAdvAction {

	private static final long serialVersionUID = 1L;

	private List<PayHourData> payHourDatas;

	private List<ChannelRtData> channelDatas;

	private List<PayMinData> payMinDatas;

	@Resource(name = "payUserService")
	private PayUserService payUserService;

	private List<AdvChartUIData1> data1;
	private List<AdvChartUIData2> data2;
	private List<AdvChartUIData3> data3;
	private List<AdvChartUIData4> data4;

	private ChartUIData lineChart1;
	private ChartUIData lineChart2;
	private ChartUIData lineChart3;
	private ChartUIData lineChart4;

	@PostConstruct
	public void init() {
		search();
	}

	public void search() {
		FacesContext context = FacesContext.getCurrentInstance();
		if (hasAgents == null || hasAgents.size() <= 0) {
			context.addMessage(null, new FacesMessage("提示", "此用户无任何代理商权限，请联系管理员配置!"));
			return;
		}
		if ((selectedAgent == null || selectedAgent.length <= 0) && !"admin".equals(session.getUserName())) {
			context.addMessage(null, new FacesMessage("提示", "请选择代理商!"));
			return;
		}

		try {
			String day = Time.getDateStr(lookDate);
			String lastday = Time.getDateByHNum(lookDate, -1);
			payHourDatas = payUserService.getListHourData(day, selectedAgent, selectedPlatform);
			channelDatas = payUserService.getListChannelData(day, selectedAffiliation, selectedChannelName,
					selectedAgent, selectedPlatform, selectedPaytype, selectedAdvPosition);

			lineChart1 = new ChartUIData();
			lineChart2 = new ChartUIData();
			lineChart3 = new ChartUIData();
			lineChart4 = new ChartUIData();
			data1 = payUserService.getAdvActDate(day, lastday, selectedAgent, selectedPlatform);
			if (data1 != null && data1.size() > 0) {
				lineChart1.setJsId("1");
				lineChart1.setJsonType(ChartType.SPLINE.type);
				lineChart1
						.setJsonStr(new ChartTool(data1, AdvChartUIData1.class, "").loadData(lineChart1.getJsonType()));
			}
			data2 = payUserService.getAdvActMacDate(day, lastday, selectedAgent, selectedPlatform);
			if (data2 != null && data2.size() > 0) {
				lineChart2.setJsId("2");
				lineChart2.setJsonType(ChartType.SPLINE.type);
				lineChart2
						.setJsonStr(new ChartTool(data2, AdvChartUIData2.class, "").loadData(lineChart2.getJsonType()));
			}
			data3 = payUserService.getAdvActUserDate(day, lastday, selectedAgent, selectedPlatform);
			if (data3 != null && data3.size() > 0) {
				lineChart3.setJsId("3");
				lineChart3.setJsonType(ChartType.SPLINE.type);
				lineChart3
						.setJsonStr(new ChartTool(data3, AdvChartUIData3.class, "").loadData(lineChart3.getJsonType()));
			}
			data4 = payUserService.getAdvActPayDate(day, lastday, selectedAgent, selectedPlatform);
			if (data4 != null && data4.size() > 0) {
				lineChart4.setJsId("4");
				lineChart4.setJsonType(ChartType.SPLINE.type);
				lineChart4
						.setJsonStr(new ChartTool(data4, AdvChartUIData4.class, "").loadData(lineChart4.getJsonType()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void searchChannelDetail(String channelName, String agentName) {
		FacesContext context = FacesContext.getCurrentInstance();
		if (hasAgents == null || hasAgents.size() <= 0) {
			context.addMessage(null, new FacesMessage("提示", "此用户无任何代理商权限，请联系管理员配置!"));
			return;
		}
		if (selectedAgent == null || selectedAgent.length <= 0) {
			context.addMessage(null, new FacesMessage("提示", "请选择代理商!"));
			return;
		}

		try {
			String day = Time.getDateStr(lookDate);
			payMinDatas = payUserService.getChannelDetailData(day, channelName, agentName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<PayMinData> getPayMinDatas() {
		return payMinDatas;
	}

	public void setPayMinDatas(List<PayMinData> payMinDatas) {
		this.payMinDatas = payMinDatas;
	}

	public void searchChannelData() {
		FacesContext context = FacesContext.getCurrentInstance();
		if (hasAgents == null || hasAgents.size() <= 0) {
			context.addMessage(null, new FacesMessage("提示", "此用户无任何代理商权限，请联系管理员配置!"));
			return;
		}
		if (selectedAgent == null || selectedAgent.length <= 0) {
			context.addMessage(null, new FacesMessage("提示", "请选择代理商!"));
			return;
		}

		try {
			String day = Time.getDateStr(lookDate);
			channelDatas = payUserService.getListChannelData(day, selectedAffiliation, selectedChannelName,
					selectedAgent, selectedPlatform, selectedPaytype, selectedAdvPosition);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<PayHourData> getPayHourDatas() {
		return payHourDatas;
	}

	public void setPayHourDatas(List<PayHourData> payHourDatas) {
		this.payHourDatas = payHourDatas;
	}

	public List<ChannelRtData> getChannelDatas() {
		return channelDatas;
	}

	public void setChannelDatas(List<ChannelRtData> channelDatas) {
		this.channelDatas = channelDatas;
	}

	public ChartUIData getLineChart1() {
		return lineChart1;
	}

	public void setLineChart1(ChartUIData lineChart1) {
		this.lineChart1 = lineChart1;
	}

	public ChartUIData getLineChart2() {
		return lineChart2;
	}

	public void setLineChart2(ChartUIData lineChart2) {
		this.lineChart2 = lineChart2;
	}

	public ChartUIData getLineChart3() {
		return lineChart3;
	}

	public void setLineChart3(ChartUIData lineChart3) {
		this.lineChart3 = lineChart3;
	}

	public ChartUIData getLineChart4() {
		return lineChart4;
	}

	public void setLineChart4(ChartUIData lineChart4) {
		this.lineChart4 = lineChart4;
	}

	public List<AdvChartUIData1> getData1() {
		return data1;
	}

	public void setData1(List<AdvChartUIData1> data1) {
		this.data1 = data1;
	}

	public List<AdvChartUIData2> getData2() {
		return data2;
	}

	public void setData2(List<AdvChartUIData2> data2) {
		this.data2 = data2;
	}

	public List<AdvChartUIData3> getData3() {
		return data3;
	}

	public void setData3(List<AdvChartUIData3> data3) {
		this.data3 = data3;
	}

	public List<AdvChartUIData4> getData4() {
		return data4;
	}

	public void setData4(List<AdvChartUIData4> data4) {
		this.data4 = data4;
	}

}
